1   //  Copyright 2008 The Apache Software Foundation
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package org.apache.tapestry5.internal.services;
16  
17  import org.apache.tapestry5.services.URLEncoder;
18  import org.testng.Assert;
19  import org.testng.annotations.DataProvider;
20  import org.testng.annotations.Test;
21  
22  public class URLEncoderImplTest extends Assert
23  {
24      private final URLEncoder encoder = new URLEncoderImpl();
25  
26      @DataProvider
27      public Object[][] encoder_inputs()
28      {
29          return new Object[][]
30                  {
31                          { "simple", "simple" },
32                          { "lettersAndNumbers123456", "lettersAndNumbers123456" },
33                          { "simplePunctuation-_.", "simplePunctuation-_." },
34                          { "a-lone-$", "a-lone-$$" },
35                          { "a-slash-/", "a-slash-$002f" },
36                          { "a-space_ _", "a-space_$0020_" },
37                          { "unicode-\u027C-", "unicode-$027c-" },
38                          { null, URLEncoderImpl.ENCODED_NULL },
39                          { "", URLEncoderImpl.ENCODED_BLANK }
40                  };
41      }
42  
43      @DataProvider
44      public Object[][] failures()
45      {
46          return new Object[][]
47                  {
48                          { "trailing-dollar-$",
49                                  "Input string 'trailing-dollar-$' is not valid; the '$' character at position 17 should be followed by another '$' or a four digit hex number (a unicode value)." },
50                          { "not-hex-after-$xyzq-",
51                                  "Input string 'not-hex-after-$xyzq-' is not valid; the '$' character at position 15 should be followed by another '$' or a four digit hex number (a unicode value)." },
52                          { "not-enough-after-$-ab",
53                                  "Input string 'not-enough-after-$-ab' is not valid; the '$' character at position 18 should be followed by another '$' or a four digit hex number (a unicode value)." },
54                          { "unsafe-@-",
55                                  "Input string 'unsafe-@-' is not valid; the character '@' at position 8 is not valid." }
56                  };
57      }
58  
59      @Test(dataProvider = "encoder_inputs")
60      public void encode(String input, String expectedOutput)
61  
62      {
63          String output = encoder.encode(input);
64  
65          assertEquals(output, expectedOutput);
66  
67          if (input != null && input.equals(output))
68              assertSame(input, output, "When no change occurs, the input object should be passed through as is.");
69      }
70  
71      @Test(dataProvider = "encoder_inputs")
72      public void decode(String expectedDecodedOutput, String encodedInput)
73  
74      {
75          String output = encoder.decode(encodedInput);
76  
77          assertEquals(output, expectedDecodedOutput);
78  
79          if (encodedInput.equals(output))
80              assertSame(encodedInput, output,
81                         "When no change occurs, the output object should be passed through as is.");
82      }
83  
84      @Test(dataProvider = "failures")
85      public void decode_failures(String input, String expectedMessage)
86      {
87          try
88          {
89              encoder.decode(input);
90              Assert.fail("This code should not be reachable.");
91          }
92          catch (IllegalArgumentException ex)
93          {
94              assertEquals(ex.getMessage(), expectedMessage);
95          }
96      }
97  }